import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/components/Login.vue'
import Home from '@/components/Home.vue'
import { getToken } from '../utils/storage'
import Welcom from '@/components/Welcom.vue'
const User = () => import('@/components/user/User.vue')
const Type = () => import('@/components/goods/Type.vue')
const goodsList = () => import('@/components/goods/List.vue')
const Add = () => import('@/components/goods/Add.vue')
const Order = () => import('@/components/order/Order.vue')

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcom',
    children: [
      {
        path: '/welcom',
        component: Welcom
      },
      {
        path: '/user',
        component: User
      },
      {
        path: '/type',
        component: Type
      },
      {
        path: '/goodsList',
        component: goodsList
      },
      {
        path: '/goods/add',
        component: Add
      },{
        path: '/order',
        component: Order
      }
    ]
  },
]

const router = new VueRouter({
  routes
})
//挂载路由导航守卫 判断跳转的页面是否需要登录
router.beforeEach((to, from, next) => {
  if (to.path === "/login") return next()
  if (!getToken()) return next('/login')
  next()
})
export default router
